package com.pansoft.openplanet.adapter;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.efounder.util.FileDeleteUtil;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.activity.AccountDetailActivity;
import com.pansoft.openplanet.activity.TransferActivity;
import com.pansoft.openplanet.bean.WalletBean;
import com.pansoft.openplanet.db.AccountDao;
import com.utilcode.util.ToastUtils;

import java.io.File;
import java.util.List;

/**
 * 我的钱包界面适配器
 * Created by will on 18-3-24.
 */

public class WalletAccountAdapter extends RecyclerView.Adapter<WalletAccountAdapter.ViewHolder> {

    private Context context;
    private List<WalletBean> wallets;
    private RefreshLister lister;
    private RequestOptions options;
    private AccountDao accountDao;

    public WalletAccountAdapter(Context context, List<WalletBean> wallets) {
        this.context = context;
        this.wallets = wallets;
        accountDao = new AccountDao(context);

        options = new RequestOptions()
                .circleCrop().error(R.drawable.contact_detail_default_avatar).placeholder(R.drawable.contact_detail_default_avatar)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.talk_chain_item_my_wallet, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        WalletBean bean = wallets.get(position);
        String address = bean.getAddress();
        if (address.length() > 20) {
            address = address.replace(address.substring(6, address.length() - 4), "****");
            holder.tvWalletAddress.setText(address);
        }
        holder.tvAssetsValue.setText(bean.getBalance());
        holder.tvAccountName.setText(bean.getAccountName());
        holder.tvSell.setOnClickListener(new ItemViewOnclick(bean));
        holder.ivSell.setOnClickListener(new ItemViewOnclick(bean));
        holder.ivCopy.setOnClickListener(new ItemViewOnclick(bean));
        holder.ivDelete.setOnClickListener(new ItemViewOnclick(bean));

        holder.ivAvatar.setOnClickListener(new ItemViewOnclick(bean));
        holder.tvAccountName.setOnClickListener(new ItemViewOnclick(bean));
        holder.tvWalletAddress.setOnClickListener(new ItemViewOnclick(bean));
        if (getItemCount()==0 || position == getItemCount()-1){
            holder.deviderView.setVisibility(View.GONE);
        }else{
            holder.deviderView.setVisibility(View.VISIBLE);
        }

        Glide.with(context)
                .load(bean.getAvatar())
                .apply(options)
                .into(holder.ivAvatar);
    }

    @Override
    public int getItemCount() {
        return wallets.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvAccountName, tvWalletAddress, tvAssetsValue, tvSell, tvBuy, tvDealDetail;
        ImageView ivCopy, ivDelete, ivSell, ivBuy, ivMore, ivAvatar;
        View deviderView;

        public ViewHolder(View itemView) {
            super(itemView);
            tvAccountName = itemView.findViewById(R.id.tv_account_name);
            tvWalletAddress = itemView.findViewById(R.id.tv_wallet_address);
            tvAssetsValue = itemView.findViewById(R.id.tv_assets_value);
            tvSell = itemView.findViewById(R.id.tv_sell);
            tvBuy = itemView.findViewById(R.id.tv_buy);
            tvDealDetail = itemView.findViewById(R.id.tv_deal_detail);
            ivCopy = itemView.findViewById(R.id.iv_copy);
            ivDelete = itemView.findViewById(R.id.iv_delete);
            ivSell = itemView.findViewById(R.id.iv_sell);
            ivBuy = itemView.findViewById(R.id.iv_buy);
            ivMore = itemView.findViewById(R.id.iv_more);
            ivAvatar = itemView.findViewById(R.id.iv_wallet);
            deviderView = itemView.findViewById(R.id.divider);
        }
    }


    public class ItemViewOnclick implements View.OnClickListener {
        private WalletBean bean;

        public ItemViewOnclick(WalletBean bean) {
            this.bean = bean;
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.tv_sell || v.getId() == R.id.iv_sell) {
                //转账界面
                Intent intent = new Intent(context, TransferActivity.class);
                intent.putExtra("myaddress", bean.getAddress());
                intent.putExtra("balance", bean.getBalance());
                intent.putExtra("filePath", bean.getFilePath());
                context.startActivity(intent);
            } else if (v.getId() == R.id.iv_copy) {
                ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(bean.getAddress());
                ToastUtils.showShort(R.string.open_planet_copy_account_to_clip_board);
            } else if (v.getId() == R.id.iv_delete) {
                new AlertDialog.Builder(context).setTitle(ResStringUtil.getString(R.string.common_text_hint)).setMessage(ResStringUtil.getString(R.string.open_planet_remove_account_confirm)).setPositiveButton(ResStringUtil.getString(R.string.common_text_confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FileDeleteUtil.delete(new File(bean.getFilePath()));
                        if (lister != null) {
                            accountDao.delete(bean.getAddress());
                            lister.refresh();
                        }
                    }
                }).setNegativeButton(R.string.common_text_cancel, null).show();
            } else if (v.getId() == R.id.tv_account_name
                    || v.getId() == R.id.tv_wallet_address
                    || v.getId() == R.id.iv_wallet) {
                //跳转详情界面
                Intent intent = new Intent(context, AccountDetailActivity.class);
                intent.putExtra("bean", bean);
                context.startActivity(intent);
            }

        }
    }

    public void setRefreshListener(RefreshLister mLister){
        this.lister =mLister;
    }
    public interface RefreshLister {
        void refresh();
    }
}